import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 3489309323@qq.com
 * @create 2020-09-14-23:56
 */
public class p5多任务组合 {
  public static ExecutorService executor = Executors.newFixedThreadPool(10);

  public static void main(String[] args) throws ExecutionException, InterruptedException {
    System.out.println("main ... start ....");

    CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
      System.out.println("查询商品图片信息");
      return "hello.jpg";
    },executor);

    CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
      System.out.println("查询商品属性信息");
      return "黑色+256G";
    },executor);

    CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
      System.out.println("查询商品介绍");
      try {
        Thread.sleep(1500);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      return "华为";
    },executor);

    /*CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
    //allOf.join();
    allOf.get(); //等待所有结果完成
    System.out.println("allOf.get(): " + allOf.get());
    System.out.println("futureImg.get():"+futureImg.get());
    System.out.println("futureAttr.get():"+futureAttr.get());
    System.out.println("futureDesc.get():"+futureDesc.get());*/
/*
main ... start ....
查询商品图片信息
查询商品属性信息
查询商品介绍
allOf.get(): null
futureImg.get():hello.jpg
futureAttr.get():黑色+256G
futureDesc.get():华为
main ... end ....
* */

    CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
    System.out.println("anyOf.get(): " + anyOf.get()); //anyOf.get(): hello.jpg
/*
main ... start ....
查询商品图片信息
查询商品属性信息
anyOf.get(): hello.jpg
main ... end ....
查询商品介绍
* */

    System.out.println("main ... end ....");
  }
}
